/*all functions working as per expectations as far as I could test them. Although I have not implemented using realloc() to resize the array. I have just incremented and decremented the pointer using p++ and p--*/
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>


int *head, *tail, *temp;
int count;

void append(int x) /*appends a value at the end of the list*/
{
*head = x;
head++;
count++;
}

void reverse() /*reverses the list*/
{
int i, *temp1, *temp2;
int c;
temp1=tail;
temp2=head;
temp2--;
for (i=0;i<(count/2);i++)
{
	c=*temp1;
	*temp1=*temp2;
	*temp2=c;
	temp1++;
	temp2--;
}
}

int pop() /*pops the last element and returns the value of the element popped*/
{
int c;
head--;
count--;
c=*head;
*head=0;
return c;
}

void remove_element(int x) /*removes 1st occurence of x from the list*/
{
int i, j, flag=0;
int *temp1;
for (i=0;i<count;i++)
{
	if (*temp==x)
	{
		count--;
		flag++;
		temp1=temp;
		temp1++;
		for (j=0;j<(count-i+1);j++)
		{
			*temp = *temp1;
			temp++;
			temp1++;
		}
		head--;
		*head = 0;
		break;
	}
	else
	{
		temp++;
	}
}
if (flag==0)
{printf("the element doesn't exist in the list\n");}
temp=tail;
}

int index_element(int x) /*returns the index value of x in the list*/
{
int i, flag=0, ind;
for (i=0;i<count;i++)
	{
	if (x==*temp)
	{flag=1;ind=i; break;}
	else
	{temp++;}
	}
temp = tail;
if (flag==0)
{return -1;}
else
{return ind;}
}

void insert(int index_val, int val) /*inserts val at index_val*/
{
int i, *temp1;
temp = (temp + index_val);
temp1=head;
head++;
while (temp1 >= temp)
	{
	*(temp1+1) = *temp1;
	temp1--;
	}
*temp = val;
count++;
temp = tail;
}

int count_element(int x) /*counts the number of occurences of x*/
{
int i;
int val_count=0;
for (i=0;i<count;i++)
	{
	if (*temp == x)
	{val_count++;}
	temp++;
	}
temp=tail;
return val_count;
}

void sort() /*sorts the list in increasing order*/
{
int i, max_val, *max_val_p, count_sort, *head_copy;
head_copy=head;
head_copy--;
count_sort = count;
max_val_p = tail;
max_val = *max_val_p;
while (count_sort>0)
	{
	for (i=0;i<count_sort;i++)
		{
		if (*temp >= max_val)
			{
				max_val_p = temp;
				max_val = *max_val_p;
			}
		temp++;
		}
	temp=tail;
	*max_val_p = *head_copy;
	*head_copy = max_val;
	head_copy--;
	max_val_p=tail;
	max_val = *max_val_p;
	count_sort--;
	}
}

void extend() /*extends the list dynamically*/
{
int d, i;
printf("enter 1 to enter an element, 0 to exit");
scanf("%d",&d);
while (d != 0)
{
	if (d==1)
	{
		printf("enter the element of the list: ");
		scanf("%d",&i);
		append(i);
		printf("enter 1 to enter an element, 0 to exit: ");
		scanf("%d",&d);
	}
	else
	{
		printf("enter 1 to enter an element, 0 to exit: ");
		scanf("%d",&d);
	}
}
}

void main()
{
int i;
int d=1;
int var_export, index_export, func_out;
head = tail = temp = malloc(sizeof(int));
while (d!=0)
{
	printf("Enter 0 to quit and print the final list\n1 to append an element\n2 to extend the list\n3 to insert an element at a position of your choice \n4 to remove 1st occurence of a certain value from list\n5 to pop the last element from the list \n6 to find out the index value of an element\n7 to count the number of occurences of a value\n8 to sort the list in increasing order\n9 to reverse the list\n10 to print the list: ");
	scanf("%d",&d);
	if (d==1)
	{
		printf("enter the number: ");
		scanf("%d",&var_export);
		append(var_export);
	}
	if (d==2)
	{
		extend();
	}
	if (d==3)
	{
		printf("enter the number: ");
		scanf("%d",&var_export);
		printf("enter the position/index value: ");
		scanf("%d",&index_export);
		insert(index_export, var_export);
	}
	if (d==4)
	{
		printf("enter the number: ");
		scanf("%d",&var_export);
		remove_element(var_export);
	}
	if (d==5)
	{
		if (count>0)
		{
			func_out = pop();
			printf("%d\n", func_out);
		}
		else	
		{
			printf("the list does not contain any element to be popped\n");
		}
	}
	if (d==6)
	{
		printf("enter the number: ");
		scanf("%d",&var_export);
		index_export = index_element(var_export);
		if (index_export==-1)
		{
			printf("the element does not exist in the list\n");
		}
		else
		{
			printf("%d\n", index_element(var_export));
		}
	}
	if (d==7)
	{
		printf("enter the number: ");
		scanf("%d",&var_export);
		printf("%d\n",count_element(var_export));
	}
	if (d==8)
	{
		sort();
	}
	if (d==9)
	{
		reverse();
	}
	if (d==10)
	{
		for (i=0;i<count;i++)
		{
			printf("%d\n", *temp);
			temp++;
		}
		temp=tail;
	}
}
for (i=0;i<count;i++)
{
	printf("%d\n", *temp);
	temp++;
}
}
